<template>
	<view class="content">
		<view class="center_top">
			<view class="center_top_left">
				<view class="date">
					{{specificDate}}
				</view>
				<view class="time">
					{{time}}
				</view>
			</view>
			<view class="center_top_right">
				<view class="frequency">
					第 {{dataInfo.rounds?dataInfo.inspectionEnd?dataInfo.rounds+1:dataInfo.rounds:"1"}} 轮 巡检
				</view>
				<text v-if="!dataInfo.inspectionEnd&&dataInfo.inspectionPeople && userInfo.realname != dataInfo.inspectionPeople"
					style="color: #ffffff;font-size: 14px;margin-left: 36rpx;">{{dataInfo.inspectionPeople}} 巡逻中</text>
				<view class="switch_but" v-else>
					<u-button @click="start" v-if="!dataInfo.id || dataInfo.inspectionEnd">开始</u-button>
					<u-button @click="end"
						v-else-if="dataInfo && !dataInfo.inspectionEnd && userInfo.realname == dataInfo.inspectionPeople">结束
					</u-button>
				</view>
			</view>
		</view>
		<view class="equipment">
			<view class="" v-for="(item,index) in equipmentList" class="equipment_item" style="display: flex;margin: 30rpx 0;padding-bottom: 20rpx;
			border-bottom: 1px solid #8f9ca2;" @click="details(item)"
				v-if="equipmentList.length > 0">
				<image src="../../static/patrol/equipment.png" mode="" class="equipment_img"></image>
				<view class="equipment_right">
					<view class="" style="display: flex;">
						<text class="name">{{item.name}}</text>
						<view class="state" v-if="item.appStatus==1">正常</view>
						<view class="state" v-if="item.appStatus==2">异常</view>
						<view class="state" v-if="item.appStatus==3">未开始</view>
					</view>
					<view class="group">设备 {{item.number ? item.number:"0"}} 组</view>
				</view>
			</view>
			<view v-if="equipmentList.length == 0" style="text-align: center;margin-top: 60rpx;">
				暂无设备~
			</view>
		</view>
		<u-modal :show="tipsShow" :title="title" :content='content' :showCancelButton="true" @confirm="jump"
			@cancel="tipsShow = false"></u-modal>
	</view>
</template>
<script>
	// import {
	//   mapState
	// } from 'vuex'
	export default {
		data() {
			return {
				specificDate: "",
				time: "",
				disabled: true,
				equipmentList: [],
				page: 1,
				limit: 20,
				userInfo: {},
				tipsShow: false,
				title: "登录提示",
				content: "你尚未登录，是否立即登录!",
				dataInfo: {},
			}
		},
		onLoad() {


		},
		// computed: {
		//   ...mapState(['hasLogin', 'userInfo'])
		// },
		onShow() {
			this.currentDate()
			this.getList()
			this.judgeLogin()
		},

		methods: {
			start() {
				let number = this.dataInfo.rounds ? this.dataInfo.inspectionEnd ? this.dataInfo.rounds + 1 : this.dataInfo
					.rounds : "1"
				uni.showModal({
					title: '提示',
					content: '确认开始第 ' + number + ' 轮的巡检？',
					showCancel: true,
					confirmText: '确定',
					success: (e) => {
						if (e.confirm) {
							this.userInfo = uni.getStorageSync("userInfo")
							this.$api.request('app.inspectionrounds', 'create', {
								inspectionPeople: this.userInfo.realname,
								rounds: this.dataInfo.rounds ? this.dataInfo.rounds + 1 : 1
							}).then(res => {
							
								this.getList()
							})
						}
					},
					fail: () => {},
					complete: () => {}
				})

			},
			end() {
				let number = this.dataInfo.rounds ? this.dataInfo.inspectionEnd ? this.dataInfo.rounds + 1 : this.dataInfo
					.rounds : "1"
				uni.showModal({
					title: '提示',
					content: '确认结束第 ' + number + ' 轮的巡检？',
					showCancel: true,
					confirmText: '确定',
					success: (e) => {
						  if (e.confirm) {
							  this.userInfo = uni.getStorageSync("userInfo")
							  this.$api.request('app.inspectionrounds', 'end', {
							  	id: this.dataInfo.id
							  }).then(res => {
							  	this.getList()
							  })
						  }
					},
					fail: () => {},
					complete: () => {}
				})
			},
			judgeLogin() { //判断是否登录
				this.userInfo = uni.getStorageSync("userInfo")
				if (!this.userInfo) {
					// this.tipsShow = true
					uni.showModal({
					  title: '登录提示',
					  content: '您尚未登录，是否立即登录？',
					  showCancel: true,
					  confirmText: '登录',
					  success: (e) => {
					    if (e.confirm) {
					      uni.navigateTo({
					        url: '/pages/public/login'
					      })
					    }
					  },
					  fail: () => {},
					  complete: () => {
					    // loginLock = false
					  }
					})
					return false
				} else {
					this.tipsShow = false
					return true
				}
			},
			getList() {
				this.$api.request('app.inspectionrounds', 'appRounds').then(res => { //查轮数
					if (res.data[0]) {
						this.dataInfo = res.data[0]
					} else {
						this.dataInfo = {}
					}
				})
				this.$api.request('app.room', 'list', {
					page: 1,
					limit: 20
				}).then(res => {
					if (res.data.items.length > 0) {
						this.equipmentList = res.data.items
					}
				})
			},
			jump() {
				this.tipsShow = false
				uni.navigateTo({
					url: '/pages/public/login'
				})
			},
			currentDate() {
				if (this.disabled) {
					let that = this
					setInterval(() => {
						let date = new Date()
						let year = date.getFullYear()
						let month = date.getMonth() + 1;
						let day = date.getDate();
						let hours = date.getHours();
						let minutes = date.getMinutes();
						let seconds = date.getSeconds() + 1;
						if (month < 10) {
							month = "0" + month
						}
						if (day < 10) {
							day = "0" + day
						}
						if (hours < 10) {
							hours = "0" + hours
						}
						if (minutes < 10) {
							minutes = "0" + minutes
						}
						if (seconds < 10) {
							seconds = "0" + seconds
						}
						that.specificDate = year + "-" + month + "-" + day
						that.time = hours + ":" + minutes + ":" + seconds
					}, 1000);
				}

			},
			details(val) {
				if (!this.judgeLogin()) {
					return
				} else {
					if (!this.dataInfo || this.dataInfo.inspectionEnd) {
						this.$api.msg("请开始巡检！")
						return
					} else {
						if (val.appStatus == 3) { //只有开始巡检才能进入跳转
							uni.navigateTo({
								url: '/pages/index/startCheck?_obj=' + JSON.stringify(val)
								// url: 'test?id=1&name=uniapp'
							});
						} else { //判断是否检查过 ，检查过就不能重复检查
							this.$api.msg("您已检查过,不能重复巡检")
							return
						}
					}
				}
			}
		},
		destroyed() {
			this.disabled = false; //销毁时清除定时器
		},
	}
</script>
<style lang="scss" scoped>
	.content {
		padding: 20rpx;
		background: #FFFFFF;
	}

	.center_top {
		width: 100%;
		display: flex;
		justify-content: space-between;
		// padding: 0 40rpx;

		.center_top_left,
		.center_top_right {
			width: 44%;
			background-color: #0eb628;
			height: 200rpx;
			border-radius: 20rpx;
			color: #ffffff;
		}

		.center_top_left {
			margin-left: 20rpx;

			.date {
				margin: 20rpx 0 20rpx 40rpx;
			}

			.time {
				text-align: center;
				font-size: 30px;
			}
		}

		.center_top_right {
			margin-right: 20rpx;

			.frequency {
				margin: 20rpx 0 20rpx 40rpx;
			}

			.switch_but {
				width: 50%;
				color: #0eb628;
				margin-left: 40rpx;
			}
		}
	}

	//设备
	.equipment {
		margin-top: 30rpx;
		padding: 0 40rpx;

		.equipment_item {
			margin-top: 40rpx;
			display: flex;

			.equipment_img {
				width: 16%;
				height: 90rpx;
			}

			.equipment_right {
				margin-left: 40rpx;
				width: 84%;
				font-size: 14px;

				.name {
					width: 80%;
				}

				.state {
					width: 20%;
					text-align: right;
				}

				.group {
					margin-top: 10rpx;
					color: #AFB3BF;
				}
			}
		}
	}
</style>
